/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.lottery.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thinkgem.jeesite.common.utils.MapListUtils;
import com.thinkgem.jeesite.modules.lottery.entity.Prize;
import com.thinkgem.jeesite.modules.lottery.service.PrizeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.lottery.entity.Participation;
import com.thinkgem.jeesite.modules.lottery.service.ParticipationService;

import java.util.HashMap;
import java.util.Map;

/**
 * 微信抽奖助手-用户参与情况Controller
 * @author 黄茂展
 * @version 2019-07-31
 */
@Controller
@RequestMapping(value = "${adminPath}/lottery/participation")
public class ParticipationController extends BaseController {

	@Autowired
	private ParticipationService participationService;
	@Autowired
	private PrizeService prizeService;
	@Autowired
	private MapListUtils mapListUtils;
	
	@ModelAttribute
	public Participation get(@RequestParam(required=false) String id) {
		Participation entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = participationService.get(id);
		}
		if (entity == null){
			entity = new Participation();
		}
		return entity;
	}
	
	@RequiresPermissions("lottery:participation:view")
	@RequestMapping(value = {"list", ""})
	public String list(Participation participation, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Participation> page = participationService.findPage(new Page<Participation>(request, response), participation); 
		model.addAttribute("page", page);
		Map<String,String> prizeMap = new HashMap<>();
		for(Prize p :mapListUtils.getPrizes()){
			prizeMap.put(p.getId(),p.getName());
		}
		model.addAttribute("prizeMap",prizeMap);
		return "modules/lottery/participationList";
	}

	@RequiresPermissions("lottery:participation:view")
	@RequestMapping(value = "form")
	public String form(Participation participation, Model model) {
		model.addAttribute("participation", participation);
		return "modules/lottery/participationForm";
	}

	@RequiresPermissions("lottery:participation:edit")
	@RequestMapping(value = "save")
	public String save(Participation participation, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, participation)){
			return form(participation, model);
		}
		participationService.save(participation);
		addMessage(redirectAttributes, "保存用户参与成功");
		return "redirect:"+Global.getAdminPath()+"/lottery/participation/?repage";
	}
	
	@RequiresPermissions("lottery:participation:edit")
	@RequestMapping(value = "delete")
	public String delete(Participation participation, RedirectAttributes redirectAttributes) {
		participationService.delete(participation);
		addMessage(redirectAttributes, "删除用户参与成功");
		return "redirect:"+Global.getAdminPath()+"/lottery/participation/?repage";
	}

}